package apigateway.zkCli;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

/**
 * Created by 小泽玛丽罗 on 2016/11/12.
 */
public class AbstractZookeeper implements Watcher {
    private  static final  int SESSION_TIME=2000;
    protected ZooKeeper zooKeeper;
    protected CountDownLatch countDownLatch=new CountDownLatch(1);

    public  void connect(String host) throws IOException, InterruptedException {
        zooKeeper=new ZooKeeper(host,SESSION_TIME,this);
        countDownLatch.await();
    }

    @Override
    public void process(WatchedEvent watchedEvent) {
        if(watchedEvent.getState()== Event.KeeperState.SyncConnected){
            countDownLatch.countDown();
            System.out.println();
        }
    }
    public  void close() throws InterruptedException {
        zooKeeper.close();
    }
}
